package work1;

import java.util.PriorityQueue;
import java.util.Queue;

public class FCFS {
	private Queue<Job> jobQueue;

	//使用优先队列的的自定义比较器，按照到达时间排序(插入之前就排序）
	public FCFS() {
		jobQueue = new PriorityQueue<>((j1, j2) -> j1.arrivalTime - j2.arrivalTime);
	}

	//作业会自动根据到达时间插入到优先队列中，确保队列中的作业按到达时间排序。
	public void addJob(Job job) {
		jobQueue.add(job);
	}

	public void schedule() {
		int currentTime = 0;
		while (!jobQueue.isEmpty()) {
			Job job = jobQueue.poll();
			System.out.println("作业名：" + job.jobName);
			System.out.println("到达时间：" + job.arrivalTime);
			System.out.println("执行时间：" + job.conductTime);

			if (currentTime < job.arrivalTime) {
				currentTime = job.arrivalTime;
			}
			currentTime += job.conductTime;

			System.out.println("作业执行结束：" + currentTime);
			System.out.println();
		}
	}

	public static void main(String[] args) {
		FCFS fcfs = new FCFS();
		fcfs.addJob(new Job("作业一", 0, 10));
		fcfs.addJob(new Job("作业二", 8, 5));
		fcfs.addJob(new Job("作业三", 6, 9));
		fcfs.addJob(new Job("作业四", 12, 5));
		fcfs.addJob(new Job("作业五", 10, 7));
		fcfs.schedule();
	}
}

